package com.gexiaobao.pigeon.ui.fragment.mine.banking

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.countDownCoroutines
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.AccountBindPhoneBean
import com.gexiaobao.pigeon.app.model.param.AccountBindPhoneParam
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.FragmentAccountUnbindPhoneBinding
import com.gexiaobao.pigeon.viewmodel.MineBankingViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 19:22
 * @Describe    : 解除绑定手机
 */
class FragmentAccountUnbindPhone : BaseFragment<MineBankingViewModel, FragmentAccountUnbindPhoneBinding>() {

    private var phone = ""

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose("解除绑定手机") {
            nav().navigateUp()
        }
        phone = arguments?.getString("mobile").toString()
        if (phone.isNotEmpty()) {
            val result = phone.substring(0, 3) + "****" + phone.substring(7, phone.length)
            mDatabind.etUnbindPhone.setText(result)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.registVerifyCodeSend, mDatabind.accountCheckTonglianSignedPhoneCommit) { it ->
            when (it) {
                mDatabind.registVerifyCodeSend -> {
                    sendVerifyCode()
                }
                mDatabind.accountCheckTonglianSignedPhoneCommit -> {
                    unBindPhone()
                }
            }
        }
    }

    private fun unBindPhone() {
        when {
            phone.isEmpty() -> showMessage(resources.getString(R.string.user_mobile_number))
            !Util.isTelPhoneNumber(phone) -> showMessage(resources.getString(R.string.user_phone_number_wrong))
            mDatabind.etUnbindPhoneVerifyCode.text?.isEmpty() == true -> showMessage(resources.getString(
                R.string.user_verification_code))
            else -> {
                val body = AccountBindPhoneBean(
                    verificationCode = mDatabind.etUnbindPhoneVerifyCode.text.toString(),
                    phone = phone
                )
                mViewModel.unbindPhone(body)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun sendVerifyCode() {
        if (phone.isEmpty()) {
            showMessage("手机号不可为空")
            return
        }
        if (!Util.isTelPhoneNumber(phone)) {
            showMessage(resources.getString(R.string.user_phone_number_wrong))
            return
        }
        job = countDownCoroutines(60, {
            mDatabind.registVerifyCodeSend.text =
                "(" + it + ")" + getString(R.string.mine_account_unbind_phone_activity_later_second_reload)
            mDatabind.registVerifyCodeSend.isEnabled = false
        }, {
            mDatabind.registVerifyCodeSend.text = "获取验证码"
            mDatabind.registVerifyCodeSend.isEnabled = true
        }, lifecycleScope)
        val paramsBody = AccountBindPhoneParam(
            type = 6,
            phone = phone
        )
        mViewModel.sendVerificationCode(paramsBody)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.verCodeResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                RxToast.showToast("发送成功")
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
        mViewModel.unBindPhoneResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                RxToast.showToast("手机号已解绑")
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }

    override fun onPause() {
        super.onPause()
        job?.cancel()
    }
}